/*
 * Map.cpp
 *
 *  Created on: Aug 26, 2011
 *      Author: mengwei.chua
 */

#include "Map.h"
#include "ParserHelper.h"

namespace projectcos
{
namespace tile
{
namespace parser
{

Map::Map()
: m_Version("")
, m_Orientation("")
, m_Width(0)
, m_Height(0)
, m_TileWidth(0)
, m_TileHeight(0)
{
	m_TileSetList.clear();
	m_LayerList.clear();
	m_ObjectGroupList.clear();
}

Map::~Map()
{
	m_TileSetList.clear();
	m_LayerList.clear();
	m_ObjectGroupList.clear();
}

bool Map::Parse(const TiXmlElement* element)
{
	if(NULL != element)
	{
		TIXML_QUERY_STRING(element, "version", m_Version);
		TIXML_QUERY_STRING(element, "orientation", m_Orientation);
		TIXML_QUERY_UNSIGNED_INT(element, "width", m_Width);
		TIXML_QUERY_UNSIGNED_INT(element, "height", m_Height);
		TIXML_QUERY_UNSIGNED_INT(element, "tilewidth", m_TileWidth);
		TIXML_QUERY_UNSIGNED_INT(element, "tileheight", m_TileHeight);

		const TiXmlElement* child = element->FirstChildElement("properties");
		if(NULL != child)
		{
			m_Properties.Parse(child);
		}

		child = element->FirstChildElement("tileset");
		while(NULL != child)
		{
			TileSet tileSet;
			if(tileSet.Parse(child))
			{
				m_TileSetList.push_back(tileSet);
			}

			child = child->NextSiblingElement("tileset");
		}

		child = element->FirstChildElement("layer");
		while(NULL != child)
		{
			Layer layer;
			if(layer.Parse(child))
			{
				m_LayerList.push_back(layer);
			}

			child = child->NextSiblingElement("layer");
		}

		child = element->FirstChildElement("objectgroup");
		while(NULL != child)
		{
			ObjectGroup objectGroup;
			if(objectGroup.Parse(child))
			{
				m_ObjectGroupList.push_back(objectGroup);
			}

			child = child->NextSiblingElement("objectgroup");
		}
	}
	else
	{
		return false;
	}

	return true;
}

std::string Map::GetVersion() const
{
	return m_Version;
}

std::string Map::GetOrientation() const
{
	return m_Orientation;
}

unsigned int Map::GetWidth() const
{
	return m_Width;
}

unsigned int Map::GetHeight() const
{
	return m_Height;
}

unsigned int Map::GetTileWidth() const
{
	return m_TileWidth;
}

unsigned int Map::GetTileHeight() const
{
	return m_TileHeight;
}

void Map::GetProperties(Properties& properties) const
{
	properties = m_Properties;
}

void Map::GetTileSets(std::deque<TileSet>& tileSets) const
{
	tileSets = m_TileSetList;
}

void Map::GetLayers(std::deque<Layer>& layers) const
{
	layers = m_LayerList;
}

void Map::GetObjectGroup(std::deque<ObjectGroup>& objectGroups) const
{
	objectGroups = m_ObjectGroupList;
}

} // parser
} // tile
} // projectcos
